package com.manage.dictionary;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.naming.NamingException;

import com.framework.Dao;
import com.util.DBUtil;

public class DictionaryDao extends Dao{
	public DictionaryVo add(DictionaryVo vo){
		String sql = "insert into t_dictionary(id, dicname, dictype, dicvalue) "+
		"	VALUES " +
		"	('"+vo.getId()+"','"+vo.getDicname()+"', '"+vo.getDictype()+"','"+vo.getDicvalue()+"')";
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = DBUtil.getConnection();
			stmt = DBUtil.getStatement(conn);
			int upCount = stmt.executeUpdate(sql);
			if (upCount!=0) {
				return vo;
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				DBUtil.replease(conn, stmt, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public DictionaryVo modify(DictionaryVo vo) {
		String setSql = this.makeSet(vo);
		if(setSql == null || "".equals(setSql))
			return null;
		
		String sql = "UPDATE t_dictionary "+ setSql +
		"	WHERE id='"+vo.getId()+"'";
		try {
			if ( 0!=DBUtil.update(sql) ) {
				return vo;
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public <T>List<T> findByType(String dictype) {
		String sql  = "select id, dicname, dictype, dicvalue " +
				"from t_dictionary " +
				"where dictype ='"+dictype+"'";
		try {
			return (List<T>) DBUtil.queryPlural(sql, new DictionaryVo());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return null;
	}
	public <T>List<T> findByTypes(List <String> dicTypeList) {
		String sql  = "select id, dicname, dictype, dicvalue " +
				"from t_dictionary " +
				"where 1=1 ";
		if(dicTypeList!=null&&dicTypeList.size()>0){
			sql+="and dictype in (";
			for(int i=0;i<dicTypeList.size();i++){
				if(i==0){
					sql+="'"+dicTypeList.get(i)+"'";
				}else{
					sql+=",'"+dicTypeList.get(i)+"'";
				}
			}
			sql+=" );";
		}
		try {
			return (List<T>) DBUtil.queryPlural(sql, new DictionaryVo());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return null;
	}
	public DictionaryVo findById(String id) {
		String sql  = "SELECT id, dicname, dictype, dicvalue " +
				"FROM t_dictionary " +
				"where id ='"+id+"'";
		try {
			return DBUtil.querySingular(sql, new DictionaryVo());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public <T>List<T> findByTypeAndDicvalue(String dictype,String dicvalue) {
		String sql  = "select id, dicname, dictype, dicvalue " +
				"from t_dictionary " +
				"where dictype ='"+dictype+"' and dicvalue ='"+dicvalue+"'";
		try {
			return (List<T>) DBUtil.queryPlural(sql, new DictionaryVo());
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
		return null;
	}
}
